package org.hank.wanandroid.ui.fragment

import android.content.Intent
import android.os.Bundle
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.fragment_mine.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.hank.commom.base.BaseBindingFragment
import org.hank.wanandroid.R
import org.hank.wanandroid.bean.IntegralBean
import org.hank.wanandroid.bean.LogoutEvent
import org.hank.wanandroid.constants.Constants
import org.hank.wanandroid.databinding.FragmentMineBinding
import org.hank.wanandroid.ui.HistoryActivity
import org.hank.wanandroid.ui.LoginActivity
import org.hank.wanandroid.ui.MyCollectActivity
import org.hank.wanandroid.ui.SettingActivity
import org.hank.wanandroid.utils.AppManager
import org.hank.wanandroid.viewmodel.MinePageViewModel


//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/4/22 11:25
 *描述:
 */
class MinFragment : BaseBindingFragment<FragmentMineBinding>(R.layout.fragment_mine) {

    private val minePageViewModel by getViewModel(MinePageViewModel::class.java) {
        item.observe(this@MinFragment) {
            dataBinding.item = it
            MMKV.defaultMMKV()?.putString(Constants.LOCAL_INTEGRAL, Gson().toJson(it))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        MMKV.defaultMMKV()?.removeValueForKey(Constants.LOCAL_INTEGRAL)
    }

    override fun onResume() {
        super.onResume()
        if (!isHidden) {
            if (AppManager.isLogin()) {
                //第一次进来 加载积分数据
                var localIntegral = Gson().fromJson(
                    MMKV.defaultMMKV()?.getString(Constants.LOCAL_INTEGRAL, ""),
                    IntegralBean::class.java
                )
                if (localIntegral != null) {
                    dataBinding.item = localIntegral
                } else {
                    minePageViewModel.getIntegral()
                }
            }
        }
        rlCollect.setOnClickListener {
            //先判断是否有登录
            if (!AppManager.isLogin()) {
                startActivity(Intent(activity, LoginActivity::class.java))
                return@setOnClickListener
            }
            startActivity(Intent(activity, MyCollectActivity::class.java))
        }
        rlSet.setOnClickListener {
            startActivity(Intent(activity, SettingActivity::class.java))
        }
        rlHistory.setOnClickListener {
            startActivity(Intent(activity, HistoryActivity::class.java))
        }
        dataBinding.mineHelper = MinHelper()
    }

    inner class MinHelper {
        fun login() {
            if (!AppManager.isLogin()) {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(event: LogoutEvent?) {
        //登出 重置页面
        minePageViewModel.item.value = null
    }
}